class QuestionItemController extends BaseController{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [questionItemInstanceList: QuestionItem.list(params), questionItemInstanceTotal: QuestionItem.count()]
    }

    def create = {
        def questionItemInstance = new QuestionItem()
        questionItemInstance.properties = params
        return [questionItemInstance: questionItemInstance]
    }

    def createall = {
        def questionItemInstance = new QuestionItem()
        questionItemInstance.properties = params
        return [questionItemInstance: questionItemInstance]
    }

    def saveall = {
		println "QuestionItem.saveall"
		println params
		String items = params.quesItems
		String[] ss = org.apache.commons.lang.StringUtils.split(items, " \t")
		boolean success = true;
		for(String s : ss){
			QuestionItem questionItemInstance = new QuestionItem()
			int splitIndex = s.indexOf(')')
			String key = s.trim().substring(0, splitIndex + 1)
			String value = s.substring(splitIndex + 1)
			questionItemInstance.quesItemId = key
			questionItemInstance.content = value
			questionItemInstance.question = Question.findById(params.question.id)
			println questionItemInstance.question
			if(saveEntity(questionItemInstance)== false) success = false
		}

        if (success) {
//            flash.message = "${message(code: 'default.created.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), questionItemInstance.id])}"
            redirect(action: "list")
        }
        else {
            render(view: "createall")
        }
    }

    def save = {
		println "QuestionItem.save"
        def questionItemInstance = new QuestionItem(params)
        if (questionItemInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), questionItemInstance.id])}"
            redirect(action: "show", id: questionItemInstance.id)
        }
        else {
            render(view: "create", model: [questionItemInstance: questionItemInstance])
        }
    }

    def show = {
        def questionItemInstance = QuestionItem.get(params.id)
        if (!questionItemInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), params.id])}"
            redirect(action: "list")
        }
        else {
            [questionItemInstance: questionItemInstance]
        }
    }

    def edit = {
        def questionItemInstance = QuestionItem.get(params.id)
        if (!questionItemInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [questionItemInstance: questionItemInstance]
        }
    }

    def update = {
        def questionItemInstance = QuestionItem.get(params.id)
        if (questionItemInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (questionItemInstance.version > version) {
                    
                    questionItemInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'questionItem.label', default: 'QuestionItem')] as Object[], "Another user has updated this QuestionItem while you were editing")
                    render(view: "edit", model: [questionItemInstance: questionItemInstance])
                    return
                }
            }
            questionItemInstance.properties = params
            if (!questionItemInstance.hasErrors() && questionItemInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), questionItemInstance.id])}"
                redirect(action: "show", id: questionItemInstance.id)
            }
            else {
                render(view: "edit", model: [questionItemInstance: questionItemInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def questionItemInstance = QuestionItem.get(params.id)
        if (questionItemInstance) {
            try {
                questionItemInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'questionItem.label', default: 'QuestionItem'), params.id])}"
            redirect(action: "list")
        }
    }
}
